﻿using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;
using ByteSite.Common;

namespace ByteSite.Web.admin.form
{
    public partial class data_edit : Web.UI.ManagePage
    {
        private string action = DTEnums.ActionEnum.Add.ToString(); //操作类型
        protected int form_id; //频道ID
        protected Entitys.dt_forms formModel; //频道实体
        private int id = 0; //内容ID
        protected string form_name = string.Empty;

        //页面初始化事件
        protected void Page_Init(object sernder, EventArgs e)
        {
            this.form_id = DTRequest.GetQueryInt("form_id");
            CreateOtherField(this.form_id); //动态生成相应的扩展字段
        }

        //页面加载事件
        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = DTRequest.GetQueryString("action");

            if (this.form_id == 0)
            {
                JscriptMsg("频道参数不正确！", "back");
                return;
            }
            this.formModel = new BLL.forms().GetModel(this.form_id); //取得频道实体

            this.form_name = formModel.name; //取得频道名称
            //如果是编辑或复制则检查信息是否存在
            if (_action == DTEnums.ActionEnum.Edit.ToString() || _action == DTEnums.ActionEnum.Copy.ToString())
            {
                this.action = _action;//修改或复制类型
                this.id = DTRequest.GetQueryInt("id");
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back");
                    return;
                }
                if (!new BLL.form().Exists(this.form_id, this.id))
                {
                    JscriptMsg("数据不存在或已被删除！", "back");
                    return;
                }
            }
            if (!Page.IsPostBack)
            {
                ChkAdminLevel("form_data_" + this.form_name, DTEnums.ActionEnum.View.ToString()); //检查权限
                ShowSysField(this.form_id); //显示相应的默认控件
                if (action == DTEnums.ActionEnum.Edit.ToString() || action == DTEnums.ActionEnum.Copy.ToString()) //修改
                {
                    ShowInfo(this.form_id, this.id);
                }
            }
        }

        #region 创建其它扩展字段=========================
        private void CreateOtherField(int _form_id)
        {
            //Expression<Func<Entitys.dt_form_attribute_field, Entitys.dt_forms_field, bool>> whereExpression
            List<Entitys.dt_form_attribute_field> ls = new BLL.form_attribute_field().GetList(_form_id, (a, s) => a.is_sys == 0);


            if (ls.Count > 0)
            {
                field_tab_content.Visible = true;
            }
            foreach (Entitys.dt_form_attribute_field modelt in ls)
            {
                //创建一个dl标签
                HtmlGenericControl htmlDL = new HtmlGenericControl("dl");
                HtmlGenericControl htmlDT = new HtmlGenericControl("dt");
                HtmlGenericControl htmlDD = new HtmlGenericControl("dd");
                if (modelt.is_required == 1)
                    htmlDT.InnerHtml = "<span style=\"color:#d60000;padding-right:5px;\">\u22c6</span>" + modelt.title;
                else
                    htmlDT.InnerHtml = modelt.title;

                switch (modelt.control_type)
                {
                    case "single-text": //单行文本
                        {
                            //创建一个TextBox控件
                            TextBox txtControl = new TextBox();
                            txtControl.ID = "field_control_" + modelt.name;
                            //CSS样式及TextMode设置
                            if (modelt.control_type == "single-text") //单行
                            {
                                txtControl.CssClass = "uk-input";
                                //是否密码框
                                if (modelt.is_password == 1)
                                {
                                    txtControl.TextMode = TextBoxMode.Password;
                                }
                            }
                            else if (modelt.control_type == "multi-text") //多行
                            {
                                txtControl.CssClass = "uk-textarea";
                                txtControl.TextMode = TextBoxMode.MultiLine;
                            }
                            else if (modelt.control_type == "number") //数字
                            {
                                txtControl.CssClass = "uk-input uk-form-width-medium";
                            }
                            else if (modelt.control_type == "datetime") //时间日期
                            {
                                txtControl.CssClass = "uk-input uk-form-width-medium  uk-input-datetime";
                                txtControl.Attributes.Add("date-format", "yyyy/MM/dd HH:mm:ss");
                                txtControl.Attributes.Add("date-range", "false");
                            }
                            else if (modelt.control_type == "date") //日期
                            {
                                txtControl.CssClass = "uk-input uk-form-width-medium  uk-input-datetime";
                                txtControl.Attributes.Add("date-format", "yyyy/MM/dd");
                                txtControl.Attributes.Add("date-range", "false");
                            }
                            else if (modelt.control_type == "image" || modelt.control_type == "video") //图片视频
                            {
                                txtControl.CssClass = "uk-input uk-form-width-large upload-path";
                            }
                            //设置默认值
                            txtControl.Text = modelt.default_value;
                            //验证提示信息
                            if (!string.IsNullOrEmpty(modelt.valid_tip_msg))
                            {
                                txtControl.Attributes.Add("tipmsg", modelt.valid_tip_msg);
                            }
                            //验证失败提示信息
                            if (!string.IsNullOrEmpty(modelt.valid_error_msg))
                            {
                                txtControl.Attributes.Add("errormsg", modelt.valid_error_msg);
                            }
                            //验证表达式
                            if (!string.IsNullOrEmpty(modelt.valid_pattern))
                            {
                                txtControl.Attributes.Add("datatype", modelt.valid_pattern);
                                txtControl.Attributes.Add("sucmsg", " ");
                            }
                            //创建一个Label控件
                            Label labelControl = new Label();
                            labelControl.CssClass = "Validform_checktip";
                            labelControl.Text = modelt.valid_tip_msg;

                            //将控件添加至DD中
                            htmlDD.Controls.Add(txtControl);
                            //如果是图片则添加上传按钮
                            if (modelt.control_type == "image")
                            {
                                HtmlGenericControl htmlBtn = new HtmlGenericControl("div");
                                htmlBtn.Attributes.Add("class", "upload-box upload-img");
                                htmlBtn.Attributes.Add("style", "margin-left:-1px;");
                                htmlDD.Controls.Add(htmlBtn);
                            }
                            //如果是视频则添加上传按钮
                            if (modelt.control_type == "video")
                            {
                                HtmlGenericControl htmlBtn = new HtmlGenericControl("div");
                                htmlBtn.Attributes.Add("class", "upload-box upload-video");
                                htmlBtn.Attributes.Add("style", "margin-left:-1px;");
                                htmlDD.Controls.Add(htmlBtn);
                            }
                            htmlDD.Controls.Add(labelControl);
                        }
                        break;
                    case "multi-text": //多行文本
                        goto case "single-text";
                    case "editor": //编辑器
                        {
                            HtmlTextArea txtTextArea = new HtmlTextArea();
                            txtTextArea.ID = "field_control_" + modelt.name;
                            //txtTextArea.Attributes.Add("style", "visibility:hidden;");
                            //是否简洁型编辑器
                            if (modelt.editor_type == 1)
                            {
                                txtTextArea.Attributes.Add("class", "editor-mini");
                            }
                            else
                            {
                                txtTextArea.Attributes.Add("class", "editor");
                            }
                            txtTextArea.Value = modelt.default_value; //默认值
                                                                      //验证提示信息
                            if (!string.IsNullOrEmpty(modelt.valid_tip_msg))
                            {
                                txtTextArea.Attributes.Add("tipmsg", modelt.valid_tip_msg);
                            }
                            //验证失败提示信息
                            if (!string.IsNullOrEmpty(modelt.valid_error_msg))
                            {
                                txtTextArea.Attributes.Add("errormsg", modelt.valid_error_msg);
                            }
                            //验证表达式
                            if (!string.IsNullOrEmpty(modelt.valid_pattern))
                            {
                                txtTextArea.Attributes.Add("datatype", modelt.valid_pattern);
                                txtTextArea.Attributes.Add("sucmsg", " ");
                            }
                            //创建一个Label控件
                            Label labelControl = new Label();
                            labelControl.CssClass = "Validform_checktip";
                            labelControl.Text = modelt.valid_tip_msg;
                            //将控件添加至DD中
                            htmlDD.Controls.Add(txtTextArea);
                            htmlDD.Controls.Add(labelControl);
                        }
                        break;
                    case "image": //图片上传
                        goto case "single-text";
                    case "multi-images": //多图上传
                        {
                            //<div class="content-img">

                            HiddenField hidControl = new HiddenField();
                            hidControl.ID = "field_control_" + modelt.name;

                            HiddenField hidControl2 = new HiddenField();
                            hidControl2.ID = "field_control_" + modelt.name + "_urls";

                            HtmlGenericControl htmlDiv = new HtmlGenericControl("div");
                            htmlDiv.Attributes.Add("id", "uploader-" + modelt.name);
                            htmlDiv.Attributes.Add("class", "multiuploader");
                            htmlDiv.Attributes.Add("form_id", _form_id.ToString());
                            htmlDiv.Attributes.Add("field_id", modelt.id.ToString());
                            htmlDiv.Attributes.Add("field_name", modelt.name);
                            htmlDiv.Attributes.Add("user_id", GetAdminInfo().id.ToString());

                            htmlDiv.Controls.Add(hidControl);
                            htmlDiv.Controls.Add(hidControl2);

                            //Label labelControl = new Label();
                            //labelControl.CssClass = "Validform_checktip";
                            //labelControl.Text = modelt.valid_tip_msg;
                            //将控件添加至DD中
                            htmlDD.Controls.Add(htmlDiv);
                            //htmlDD.Controls.Add(labelControl);
                        }
                        break;
                    case "video": //视频上传
                        goto case "single-text";
                    case "number": //数字
                        goto case "single-text";
                    case "datetime": //时间日期
                        goto case "single-text";
                    case "date": //日期
                        goto case "single-text";
                    case "checkbox": //复选框
                        {
                            CheckBox cbControl = new CheckBox();
                            cbControl.ID = "field_control_" + modelt.name;
                            //默认值
                            if (modelt.default_value == "1")
                            {
                                cbControl.Checked = true;
                            }
                            HtmlGenericControl htmlDiv = new HtmlGenericControl("div");
                            htmlDiv.Attributes.Add("class", "rule-single-checkbox");
                            htmlDiv.Controls.Add(cbControl);
                            //将控件添加至DD中
                            htmlDD.Controls.Add(htmlDiv);
                            if (!string.IsNullOrEmpty(modelt.valid_tip_msg))
                            {
                                //创建一个Label控件
                                Label labelControl = new Label();
                                labelControl.CssClass = "Validform_checktip";
                                labelControl.Text = modelt.valid_tip_msg;
                                htmlDD.Controls.Add(labelControl);
                            }
                        }
                        break;
                    case "multi-radio": //多项单选
                        {
                            RadioButtonList rblControl = new RadioButtonList();
                            rblControl.ID = "field_control_" + modelt.name;
                            rblControl.RepeatDirection = RepeatDirection.Horizontal;
                            rblControl.RepeatLayout = RepeatLayout.Flow;
                            HtmlGenericControl htmlDiv = new HtmlGenericControl("div");
                            htmlDiv.Attributes.Add("class", "rule-multi-radio");
                            htmlDiv.Controls.Add(rblControl);
                            //赋值选项
                            if (Utils.ObjToInt(modelt.item_datasource) == 0)
                            {
                                string[] valArr = modelt.item_option.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
                                for (int i = 0; i < valArr.Length; i++)
                                {
                                    string[] valItemArr = valArr[i].Split('|');
                                    if (valItemArr.Length == 2)
                                    {
                                        rblControl.Items.Add(new ListItem(valItemArr[0], valItemArr[1]));
                                    }
                                }
                            }
                            else
                            {
                                Entitys.dt_custom_list customListModel = new BLL.custom_list().GetModel(Utils.ObjToInt(modelt.item_datasource));
                                if (customListModel != null)
                                {
                                    List<Entitys.list_item> list = new BLL.list_item().GetList(customListModel.table, customListModel.key, customListModel.value, null);
                                    foreach (Entitys.list_item item in list)
                                    {
                                        rblControl.Items.Add(new ListItem(item.key, item.value));
                                    }
                                }

                            }
                            rblControl.SelectedValue = modelt.default_value; //默认值
                                                                             //创建一个Label控件
                            Label labelControl = new Label();
                            labelControl.CssClass = "Validform_checktip";
                            labelControl.Text = modelt.valid_tip_msg;
                            //将控件添加至DD中
                            htmlDD.Controls.Add(htmlDiv);
                            htmlDD.Controls.Add(labelControl);
                        }
                        break;
                    case "multi-checkbox": //多项多选
                        {
                            CheckBoxList cblControl = new CheckBoxList();
                            cblControl.ID = "field_control_" + modelt.name;
                            cblControl.RepeatDirection = RepeatDirection.Horizontal;
                            cblControl.RepeatLayout = RepeatLayout.Flow;
                            HtmlGenericControl htmlDiv = new HtmlGenericControl("div");
                            htmlDiv.Attributes.Add("class", "rule-multi-checkbox");
                            htmlDiv.Controls.Add(cblControl);
                            //赋值选项
                            if (Utils.ObjToInt(modelt.item_datasource) == 0)
                            {
                                string[] valArr = modelt.item_option.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
                                for (int i = 0; i < valArr.Length; i++)
                                {
                                    string[] valItemArr = valArr[i].Split('|');
                                    if (valItemArr.Length == 2)
                                    {
                                        cblControl.Items.Add(new ListItem(valItemArr[0], valItemArr[1]));
                                    }
                                }
                            }
                            else
                            {
                                Entitys.dt_custom_list customListModel = new BLL.custom_list().GetModel(Utils.ObjToInt(modelt.item_datasource));
                                if (customListModel != null)
                                {
                                    List<Entitys.list_item> list = new BLL.list_item().GetList(customListModel.table, customListModel.key, customListModel.value, null);
                                    foreach (Entitys.list_item item in list)
                                    {
                                        cblControl.Items.Add(new ListItem(item.key, item.value));
                                    }
                                }

                            }
                            cblControl.SelectedValue = modelt.default_value; //默认值
                                                                             //创建一个Label控件
                            Label labelControl = new Label();
                            labelControl.CssClass = "Validform_checktip";
                            labelControl.Text = modelt.valid_tip_msg;
                            //将控件添加至DD中
                            htmlDD.Controls.Add(htmlDiv);
                            htmlDD.Controls.Add(labelControl);
                        }
                        break;

                    case "dropdownlist": //下拉菜单
                        {
                            DropDownList ddlControl = new DropDownList();
                            ddlControl.ID = "field_control_" + modelt.name;
                            ddlControl.CssClass = "uk-select";
                            HtmlGenericControl htmlDiv = new HtmlGenericControl("div");
                            htmlDiv.Controls.Add(ddlControl);
                            //赋值选项
                            if (Utils.ObjToInt(modelt.item_datasource) == 0)
                            {
                                string[] valArr = modelt.item_option.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
                                for (int i = 0; i < valArr.Length; i++)
                                {
                                    string[] valItemArr = valArr[i].Split('|');
                                    if (valItemArr.Length == 2)
                                    {
                                        ddlControl.Items.Add(new ListItem(valItemArr[0], valItemArr[1]));
                                    }
                                }
                            }
                            else
                            {
                                Entitys.dt_custom_list customListModel = new BLL.custom_list().GetModel(Utils.ObjToInt(modelt.item_datasource));
                                if (customListModel != null)
                                {
                                    List<Entitys.list_item> list = new BLL.list_item().GetList(customListModel.table, customListModel.key, customListModel.value, null);
                                    foreach (Entitys.list_item item in list)
                                    {
                                        ddlControl.Items.Add(new ListItem(item.key, item.value));
                                    }
                                }

                            }
                            ddlControl.SelectedValue = modelt.default_value; //默认值
                                                                             //创建一个Label控件
                            Label labelControl = new Label();
                            labelControl.CssClass = "Validform_checktip";
                            labelControl.Text = modelt.valid_tip_msg;
                            //将控件添加至DD中
                            htmlDD.Controls.Add(htmlDiv);
                            htmlDD.Controls.Add(labelControl);
                        }
                        break;
                    case "multi-dropdownlist": //多选下拉菜单
                        {
                            //<div class="xm-select">
                            //    <asp:DropDownList id="ddlTags" runat="server" CssClass="uk-select" datatype="*" errormsg="请选择标签" sucmsg=" "></asp:DropDownList>
                            //    <asp:HiddenField ID="hidXmSelectddlTags" runat="server" />
                            //</div>
                            DropDownList ddlControl = new DropDownList();
                            ddlControl.ID = "field_control_" + modelt.name;
                            ddlControl.CssClass = "uk-select";
                            HiddenField hidControl = new HiddenField();
                            hidControl.ID = "hidXmSelect" + ddlControl.ID;

                            HtmlGenericControl htmlDiv = new HtmlGenericControl("div");
                            htmlDiv.Attributes.Add("class", "xm-select");

                            htmlDiv.Controls.Add(ddlControl);
                            htmlDiv.Controls.Add(hidControl);
                            //赋值选项
                            if (Utils.ObjToInt(modelt.item_datasource) == 0)
                            {
                                string[] valArr = modelt.item_option.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
                                for (int i = 0; i < valArr.Length; i++)
                                {
                                    string[] valItemArr = valArr[i].Split('|');
                                    if (valItemArr.Length == 2)
                                    {
                                        ddlControl.Items.Add(new ListItem(valItemArr[0], valItemArr[1]));
                                    }
                                }
                            }
                            else
                            {
                                Entitys.dt_custom_list customListModel = new BLL.custom_list().GetModel(Utils.ObjToInt(modelt.item_datasource));
                                if (customListModel != null)
                                {
                                    List<Entitys.list_item> list = new BLL.list_item().GetList(customListModel.table, customListModel.key, customListModel.value, null);
                                    foreach (Entitys.list_item item in list)
                                    {
                                        ddlControl.Items.Add(new ListItem(item.key, item.value));
                                    }
                                }

                            }
                            ddlControl.SelectedValue = modelt.default_value; //默认值
                                                                             //创建一个Label控件
                            Label labelControl = new Label();
                            labelControl.CssClass = "Validform_checktip";
                            labelControl.Text = modelt.valid_tip_msg;
                            //将控件添加至DD中
                            htmlDD.Controls.Add(htmlDiv);
                            htmlDD.Controls.Add(labelControl);
                        }
                        break;
                }

                //将DT和DD添加到DL中
                htmlDL.Controls.Add(htmlDT);
                htmlDL.Controls.Add(htmlDD);
                //将DL添加至field_tab_content中
                field_tab_content.Controls.Add(htmlDL);
            }
        }
        #endregion

        #region 显示默认扩展字段=========================
        private void ShowSysField(int _form_id)
        {
            //查找该频道所选的默认字段
            List<Entitys.dt_form_attribute_field> ls = new BLL.form_attribute_field().GetModelList(_form_id, (a, s) => a.is_sys == 1);
            foreach (Entitys.dt_form_attribute_field modelt in ls)
            {
                //查找相应的控件，如找到则显示
                HtmlGenericControl htmlDiv = FindControl("div_" + modelt.name) as HtmlGenericControl;
                if (htmlDiv != null)
                {
                    htmlDiv.Visible = true;
                    ((Label)htmlDiv.FindControl("div_" + modelt.name + "_title")).Text = modelt.title;
                    ((TextBox)htmlDiv.FindControl("field_control_" + modelt.name)).Text = modelt.default_value;
                    //((Label)htmlDiv.FindControl("div_" + modelt.name + "_tip")).Text = modelt.valid_tip_msg;
                }
            }
        }
        #endregion

        #region 赋值操作=================================
        private void ShowInfo(int _form_id, int _id)
        {
            BLL.form bll = new BLL.form();
            Entitys.form model = bll.GetModel(_form_id, _id);

            txtDataName.Text = model.data_name;

            txtSortId.Text = model.sort_id.ToString();
            //扩展字段赋值
            List<Entitys.dt_form_attribute_field> ls1 = new BLL.form_attribute_field().GetModelList(this.form_id, (a, s) => true);
            foreach (Entitys.dt_form_attribute_field modelt1 in ls1)
            {
                switch (modelt1.control_type)
                {
                    case "single-text": //单行文本
                        {
                            TextBox txtControl = FindControl("field_control_" + modelt1.name) as TextBox;
                            if (txtControl != null && model.fields.ContainsKey(modelt1.name))
                            {
                                if (modelt1.is_password == 1)
                                {
                                    txtControl.Attributes.Add("value", Utils.ObjectToStr(model.fields[modelt1.name]));
                                }
                                else
                                {
                                    txtControl.Text = Utils.ObjectToStr(model.fields[modelt1.name]);
                                }
                            }
                        }
                        break;
                    case "multi-text": //多行文本
                        goto case "single-text";
                    case "editor": //编辑器
                        {
                            HtmlTextArea txtAreaControl = FindControl("field_control_" + modelt1.name) as HtmlTextArea;
                            if (txtAreaControl != null && model.fields.ContainsKey(modelt1.name))
                            {
                                txtAreaControl.Value = Utils.ObjectToStr(model.fields[modelt1.name]);
                            }
                        }
                        break;
                    case "image": //图片上传
                        goto case "single-text";
                    case "video": //视频上传
                        goto case "single-text";
                    case "number": //数字
                        goto case "single-text";
                    case "datetime": //时间日期
                        {
                            TextBox txtControl = FindControl("field_control_" + modelt1.name) as TextBox;
                            if (txtControl != null && model.fields.ContainsKey(modelt1.name))
                            {
                                txtControl.Attributes.Add("autocomplete", "off");
                                if (model.fields[modelt1.name] == null)
                                {
                                    txtControl.Text = "";
                                }
                                else
                                {
                                    txtControl.Text = Utils.ObjectToDateTime(model.fields[modelt1.name]).ToString("yyyy/MM/dd HH:mm:ss");
                                }
                            }
                        }
                        break;
                    case "date": //日期
                        {
                            TextBox txtControl = FindControl("field_control_" + modelt1.name) as TextBox;
                            if (txtControl != null && model.fields.ContainsKey(modelt1.name))
                            {
                                txtControl.Attributes.Add("autocomplete", "off");
                                if (model.fields[modelt1.name] == null)
                                {
                                    txtControl.Text = "";
                                }
                                else
                                {
                                    txtControl.Text = Utils.ObjectToDateTime(model.fields[modelt1.name]).ToString("yyyy/MM/dd");
                                }
                            }
                        }
                        break;
                    case "checkbox": //复选框
                        {
                            CheckBox cbControl = FindControl("field_control_" + modelt1.name) as CheckBox;
                            if (cbControl != null && model.fields.ContainsKey(modelt1.name))
                            {
                                if (model.fields[modelt1.name].ToString() == "1")
                                {
                                    cbControl.Checked = true;
                                }
                                else
                                {
                                    cbControl.Checked = false;
                                }
                            }
                        }
                        break;
                    case "multi-radio": //多项单选
                        {
                            RadioButtonList rblControl = FindControl("field_control_" + modelt1.name) as RadioButtonList;
                            if (rblControl != null && model.fields.ContainsKey(modelt1.name))
                            {
                                rblControl.SelectedValue = Utils.ObjectToStr(model.fields[modelt1.name]);
                            }
                        }
                        break;
                    case "multi-checkbox": //多项多选
                        {
                            CheckBoxList cblControl = FindControl("field_control_" + modelt1.name) as CheckBoxList;
                            if (cblControl != null && model.fields.ContainsKey(modelt1.name))
                            {
                                string[] valArr = Utils.ObjectToStr(model.fields[modelt1.name]).Split(',');
                                for (int i = 0; i < cblControl.Items.Count; i++)
                                {
                                    cblControl.Items[i].Selected = false; //先取消默认的选中
                                    foreach (string str in valArr)
                                    {
                                        if (cblControl.Items[i].Value == str)
                                        {
                                            cblControl.Items[i].Selected = true;
                                        }
                                    }
                                }
                            }
                        }
                        break;

                    case "dropdownlist": //下拉菜单
                        {
                            DropDownList ddlControl = FindControl("field_control_" + modelt1.name) as DropDownList;
                            if (ddlControl != null && model.fields.ContainsKey(modelt1.name))
                            {
                                ddlControl.SelectedValue = Utils.ObjectToStr(model.fields[modelt1.name]);
                            }
                        }
                        break;

                    case "multi-dropdownlist": //多项多选
                        {
                            HiddenField hidControl = FindControl("hidXmSelectfield_control_" + modelt1.name) as HiddenField;
                            if (hidControl != null && model.fields.ContainsKey(modelt1.name))
                            {
                                hidControl.Value = Utils.ObjectToStr(model.fields[modelt1.name]);
                            }
                        }
                        break;
                    case "multi-images": //多图上传
                        {
                            HiddenField hidControl = FindControl("field_control_" + modelt1.name) as HiddenField;
                            HiddenField hidControl2 = FindControl("field_control_" + modelt1.name+"_urls") as HiddenField;
                            if (hidControl != null && model.fields.ContainsKey(modelt1.name))
                            {
                                string value = string.Empty;
                                string urls = string.Empty;
                                BLL.form_data_file_list fileBll = new BLL.form_data_file_list();
                                List<Entitys.dt_form_data_file_list> fileList = fileBll.GetList(x => x.form_id == _form_id && x.field_id == modelt1.id && x.data_id == _id && x.delete_flag == 0, x => x.id);
                                if (fileList.Count > 0)
                                {
                                    foreach (Entitys.dt_form_data_file_list file in fileList)
                                    {
                                        if (string.IsNullOrEmpty(value))
                                        {
                                            value = file.guid;
                                            urls = file.url;
                                        }
                                        else
                                        {
                                            value = value + "|" + file.guid;
                                            urls = urls + "|" + file.url;
                                        }

                                    }
                                }
                                hidControl2.Value = urls;
                                hidControl.Value = value;
                            }
                        }
                        break;
                }
            }
        }
        #endregion

        #region 扩展字段赋值=============================
        private Dictionary<string, object> SetFieldValues(int _form_id, out string msg)
        {
            msg = string.Empty;
            List<Entitys.dt_form_attribute_field> list = new BLL.form_attribute_field().GetList(_form_id, (a, s) => true);
            Dictionary<string, object> dic = new Dictionary<string, object>();
            foreach (Entitys.dt_form_attribute_field f in list)
            {
                //查找相应的控件
                switch (f.control_type.ToString())
                {
                    case "single-text": //单行文本
                        {
                            TextBox txtControl = FindControl("field_control_" + f.name.ToString()) as TextBox;
                            if (txtControl != null)
                            {
                                if (f.is_required == 1 && !string.IsNullOrEmpty(txtControl.Text.Trim()))
                                {
                                    if (f.default_value == "{userinfo.id}")
                                    {
                                        txtControl.Text = GetAdminInfo().id.ToString();
                                    }
                                    else if (f.default_value == "{userinfo.role_id}")
                                    {
                                        txtControl.Text = GetAdminInfo().role_id.ToString();
                                    }

                                    if (Common.Validator.IsMatch(txtControl.Text.Trim(), f.valid_pattern.Substring(1, f.valid_pattern.Length - 2)))
                                    {
                                        dic.Add(f.name.ToString(), txtControl.Text.Trim());
                                    }
                                    else
                                    {
                                        msg = f.title + "填写不符合规则" + (string.IsNullOrEmpty(f.valid_tip_msg) ? "" : "," + f.valid_tip_msg);
                                    }
                                }
                                else if (f.is_required == 1 && string.IsNullOrEmpty(txtControl.Text.Trim()))
                                {
                                    msg = f.title + "不能为空";
                                }
                                else
                                {
                                    if (!string.IsNullOrEmpty(f.valid_pattern))
                                    {
                                        if (f.default_value == "{userinfo.id}")
                                        {
                                            txtControl.Text = GetAdminInfo().id.ToString();
                                        }
                                        else if (f.default_value == "{userinfo.role_id}")
                                        {
                                            txtControl.Text = GetAdminInfo().role_id.ToString();
                                        }
                                        if (Common.Validator.IsMatch(txtControl.Text.Trim().ToString(), f.valid_pattern.Substring(1, f.valid_pattern.Length - 2)))
                                        {
                                            dic.Add(f.name.ToString(), txtControl.Text.Trim().ToString());
                                        }
                                        else
                                        {
                                            msg = f.title + "填写不符合规则" + (string.IsNullOrEmpty(f.valid_tip_msg) ? "" : "," + f.valid_tip_msg);
                                        }
                                    }
                                    else
                                    {
                                        dic.Add(f.name.ToString(), txtControl.Text.Trim().ToString());
                                    }
                                }
                            }
                        }
                        break;
                    case "multi-text": //多行文本
                        goto case "single-text";
                    case "editor": //编辑器
                        {
                            HtmlTextArea htmlTextAreaControl = FindControl("field_control_" + f.name.ToString()) as HtmlTextArea;
                            if (htmlTextAreaControl != null)
                            {
                                if (f.is_required == 1 && !string.IsNullOrEmpty(htmlTextAreaControl.Value)) {
                                    if (Common.Validator.IsMatch(htmlTextAreaControl.Value, f.valid_pattern.Substring(1, f.valid_pattern.Length - 2)))
                                    {
                                        dic.Add(f.name.ToString(), htmlTextAreaControl.Value);
                                    }
                                    else
                                    {
                                        msg = f.title + "填写不符合规则" + (string.IsNullOrEmpty(f.valid_tip_msg) ? "" : "," + f.valid_tip_msg);
                                    }
                                }
                                else if (f.is_required == 1 && string.IsNullOrEmpty(htmlTextAreaControl.Value))
                                {
                                    msg = f.title + "不能为空";
                                }
                                else
                                {
                                    if (!string.IsNullOrEmpty(f.valid_pattern))
                                    {
                                        if (Common.Validator.IsMatch(htmlTextAreaControl.Value.ToString(), f.valid_pattern.Substring(1, f.valid_pattern.Length - 2)))
                                        {
                                            dic.Add(f.name.ToString(), htmlTextAreaControl.Value.ToString());
                                        }
                                        else
                                        {
                                            msg = f.title + "填写不符合规则" + (string.IsNullOrEmpty(f.valid_tip_msg) ? "" : "," + f.valid_tip_msg);
                                        }
                                    }
                                    else
                                    {
                                        dic.Add(f.name.ToString(), htmlTextAreaControl.Value.ToString());
                                    }
                                }
                            }
                        }
                        break;
                    case "image": //图片上传
                        goto case "single-text";
                    case "video": //视频上传
                        goto case "single-text";
                    case "number": //数字
                        goto case "single-text";
                    case "datetime": //时间日期
                        goto case "single-text";
                    case "date": //日期
                        goto case "single-text";
                    case "checkbox": //复选框
                        {
                            CheckBox cbControl = FindControl("field_control_" + f.name.ToString()) as CheckBox;
                            if (cbControl != null)
                            {
                                if (cbControl.Checked == true)
                                {
                                    dic.Add(f.name.ToString(), "1");
                                }
                                else
                                {
                                    dic.Add(f.name.ToString(), "0");
                                }
                            }
                        }
                        break;
                    case "multi-radio": //多项单选
                        {
                            RadioButtonList rblControl = FindControl("field_control_" + f.name.ToString()) as RadioButtonList;
                            if (rblControl != null)
                            {
                                if (f.is_required == 1 && !string.IsNullOrEmpty(rblControl.SelectedValue))
                                {
                                    if (f.default_value == "{userinfo.id}")
                                    {
                                        rblControl.SelectedValue = GetAdminInfo().id.ToString();
                                    }
                                    else if (f.default_value == "{userinfo.role_id}")
                                    {
                                        rblControl.SelectedValue = GetAdminInfo().role_id.ToString();
                                    }
                                    if (Common.Validator.IsMatch(rblControl.SelectedValue, f.valid_pattern.Substring(1, f.valid_pattern.Length - 2)))
                                    {
                                        dic.Add(f.name.ToString(), rblControl.SelectedValue);
                                    }
                                    else
                                    {
                                        msg = f.title + "填写不符合规则" + (string.IsNullOrEmpty(f.valid_tip_msg) ? "" : "," + f.valid_tip_msg);
                                    }
                                }
                                else if (f.is_required == 1 && string.IsNullOrEmpty(rblControl.SelectedValue.ToString()))
                                {
                                    msg = f.title + "不能为空";
                                }
                                else
                                {
                                    if (!string.IsNullOrEmpty(f.valid_pattern))
                                    {
                                        if (f.default_value == "{userinfo.id}")
                                        {
                                            rblControl.SelectedValue = GetAdminInfo().id.ToString();
                                        }
                                        else if (f.default_value == "{userinfo.role_id}")
                                        {
                                            rblControl.SelectedValue = GetAdminInfo().role_id.ToString();
                                        }
                                        if (Common.Validator.IsMatch(rblControl.SelectedValue.ToString(), f.valid_pattern.Substring(1, f.valid_pattern.Length - 2)))
                                        {
                                            dic.Add(f.name.ToString(), rblControl.SelectedValue.ToString());
                                        }
                                        else
                                        {
                                            msg = f.title + "填写不符合规则" + (string.IsNullOrEmpty(f.valid_tip_msg) ? "" : "," + f.valid_tip_msg);
                                        }
                                    }
                                    else
                                    {
                                        dic.Add(f.name.ToString(), rblControl.SelectedValue.ToString());
                                    }
                                }
                            }
                        }
                        break;
                    case "multi-checkbox": //多项多选
                        {
                            CheckBoxList cblControl = FindControl("field_control_" + f.name.ToString()) as CheckBoxList;
                            if (cblControl != null)
                            {
                                StringBuilder tempStr = new StringBuilder();
                                for (int i = 0; i < cblControl.Items.Count; i++)
                                {
                                    if (cblControl.Items[i].Selected)
                                    {
                                        tempStr.Append(cblControl.Items[i].Value.Replace(',', '，') + ",");
                                    }
                                }

                                if (f.is_required == 1 && !string.IsNullOrEmpty(Utils.DelLastComma(tempStr.ToString())))
                                {

                                    if (f.default_value == "{userinfo.id}")
                                    {
                                        tempStr.Clear().Append(GetAdminInfo().id.ToString());
                                    }
                                    else if (f.default_value == "{userinfo.role_id}")
                                    {
                                        tempStr.Clear().Append(GetAdminInfo().role_id.ToString());
                                    }

                                    if (Common.Validator.IsMatch(Utils.DelLastComma(tempStr.ToString()), f.valid_pattern.Substring(1, f.valid_pattern.Length - 2)))
                                    {
                                        dic.Add(f.name.ToString(), Utils.DelLastComma(tempStr.ToString()));
                                    }
                                    else
                                    {
                                        msg = f.title + "填写不符合规则" + (string.IsNullOrEmpty(f.valid_tip_msg) ? "" : "," + f.valid_tip_msg);
                                    }
                                }
                                else if (f.is_required == 1 && string.IsNullOrEmpty(Utils.DelLastComma(tempStr.ToString())))
                                {
                                    msg = f.title + "不能为空";
                                }
                                else
                                {
                                    if (!string.IsNullOrEmpty(f.valid_pattern))
                                    {

                                        if (f.default_value == "{userinfo.id}")
                                        {
                                            tempStr.Clear().Append(GetAdminInfo().id.ToString());
                                        }
                                        else if (f.default_value == "{userinfo.role_id}")
                                        {
                                            tempStr.Clear().Append(GetAdminInfo().role_id.ToString());
                                        }

                                        if (Common.Validator.IsMatch(Utils.DelLastComma(tempStr.ToString()), f.valid_pattern.Substring(1, f.valid_pattern.Length - 2)))
                                        {
                                            dic.Add(f.name.ToString(), Utils.DelLastComma(tempStr.ToString()));
                                        }
                                        else
                                        {
                                            msg = f.title + "填写不符合规则" + (string.IsNullOrEmpty(f.valid_tip_msg) ? "" : "," + f.valid_tip_msg);
                                        }
                                    }
                                    else
                                    {
                                        dic.Add(f.name.ToString(), Utils.DelLastComma(tempStr.ToString()));
                                    }
                                }
                            }
                        }
                        break;
                    case "dropdownlist": //下拉单选
                        {
                            DropDownList ddlControl = FindControl("field_control_" + f.name.ToString()) as DropDownList;
                            if (ddlControl != null)
                            {
                                if (f.is_required == 1 && !string.IsNullOrEmpty(ddlControl.SelectedValue))
                                {
                                    if (f.default_value == "{userinfo.id}")
                                    {
                                        ddlControl.SelectedValue = GetAdminInfo().id.ToString();
                                    }
                                    else if (f.default_value == "{userinfo.role_id}")
                                    {
                                        ddlControl.SelectedValue = GetAdminInfo().role_id.ToString();
                                    }

                                    if (Common.Validator.IsMatch(ddlControl.SelectedValue, f.valid_pattern.Substring(1, f.valid_pattern.Length - 2)))
                                    {
                                        dic.Add(f.name.ToString(), ddlControl.SelectedValue);
                                    }
                                    else
                                    {
                                        msg = f.title + "填写不符合规则" + (string.IsNullOrEmpty(f.valid_tip_msg) ? "" : "," + f.valid_tip_msg);
                                    }
                                }
                                else if (f.is_required == 1 && string.IsNullOrEmpty(ddlControl.SelectedValue))
                                {
                                    msg = f.title + "不能为空";
                                }
                                else
                                {
                                    if (!string.IsNullOrEmpty(f.valid_pattern))
                                    {
                                        if (f.default_value == "{userinfo.id}")
                                        {
                                            ddlControl.SelectedValue = GetAdminInfo().id.ToString();
                                        }
                                        else if (f.default_value == "{userinfo.role_id}")
                                        {
                                            ddlControl.SelectedValue = GetAdminInfo().role_id.ToString();
                                        }
                                        if (Common.Validator.IsMatch(ddlControl.SelectedValue.ToString(), f.valid_pattern.Substring(1, f.valid_pattern.Length - 2)))
                                        {
                                            dic.Add(f.name.ToString(), ddlControl.SelectedValue.ToString());
                                        }
                                        else
                                        {
                                            msg = f.title + "填写不符合规则" + (string.IsNullOrEmpty(f.valid_tip_msg) ? "" : "," + f.valid_tip_msg);
                                        }
                                    }
                                    else
                                    {
                                        dic.Add(f.name.ToString(), ddlControl.SelectedValue.ToString());
                                    }
                                }
                            }
                        }
                        break;
                    case "multi-dropdownlist": //多选菜单
                        {
                            HiddenField hidControl = FindControl("hidXmSelectfield_control_" + f.name.ToString()) as HiddenField;
                            if (hidControl != null)
                            {
                                if (f.is_required == 1 && !string.IsNullOrEmpty(hidControl.Value))
                                {
                                    if (f.default_value == "{userinfo.id}")
                                    {
                                        hidControl.Value = GetAdminInfo().id.ToString();
                                    }
                                    else if (f.default_value == "{userinfo.role_id}")
                                    {
                                        hidControl.Value = GetAdminInfo().role_id.ToString();
                                    }
                                    if (Common.Validator.IsMatch(hidControl.Value.ToString(), f.valid_pattern.Substring(1, f.valid_pattern.Length-2)))
                                    {
                                        dic.Add(f.name.ToString(), hidControl.Value.ToString());
                                    }
                                    else
                                    {
                                        msg = f.title + "填写不符合规则" + (string.IsNullOrEmpty(f.valid_tip_msg) ? "" : "," + f.valid_tip_msg);
                                    }

                                }
                                else if (f.is_required == 1 && string.IsNullOrEmpty(hidControl.Value))
                                {
                                    msg = f.title + "不能为空";
                                }
                                else
                                {
                                    if (!string.IsNullOrEmpty(f.valid_pattern))
                                    {
                                        if (f.default_value == "{userinfo.id}")
                                        {
                                            hidControl.Value = GetAdminInfo().id.ToString();
                                        }
                                        else if (f.default_value == "{userinfo.role_id}")
                                        {
                                            hidControl.Value = GetAdminInfo().role_id.ToString();
                                        }
                                        if (Common.Validator.IsMatch(hidControl.Value.ToString(), f.valid_pattern.Substring(1, f.valid_pattern.Length - 2)))
                                        {
                                            dic.Add(f.name.ToString(), hidControl.Value.ToString());
                                        }
                                        else
                                        {
                                            msg = f.title + "填写不符合规则" + (string.IsNullOrEmpty(f.valid_tip_msg) ? "" : "," + f.valid_tip_msg);
                                        }
                                    }
                                    else
                                    {
                                        dic.Add(f.name.ToString(), hidControl.Value.ToString());
                                    }
                                }
                            }
                        }
                        break;
                    case "multi-images": //多图上传
                        {
                            HiddenField hidControl = FindControl("field_control_" + f.name.ToString()) as HiddenField;
                            if (hidControl != null)
                            {
                                BLL.form_data_file_list bll = new BLL.form_data_file_list();
                                List<Entitys.dt_form_data_file_list> fileList = bll.GetList(x => x.form_id == _form_id && x.field_id == f.id && x.data_id == this.id && x.delete_flag == 0, x => x.id);
                                if (fileList.Count > 0)
                                {
                                    string[] guids = hidControl.Value.Split('|');

                                    foreach (Entitys.dt_form_data_file_list file in fileList)
                                    {
                                        bool notDel = false;
                                        foreach (string guid in guids)
                                        {
                                            if (file.guid == guid)
                                            {
                                                notDel = true;
                                            }
                                        }
                                        if (!notDel)
                                        {
                                            bll.UpdateField(x => new Entitys.dt_form_data_file_list() { delete_flag = 1 }, x => x.guid == file.guid);
                                        }
                                    }
                                }
                                else
                                {
                                    string[] guids = hidControl.Value.Split('|');
                                    foreach (string guid in guids)
                                    {
                                        bll.UpdateField(x => new Entitys.dt_form_data_file_list() { data_id = this.id, delete_flag = 0 }, x => x.guid == guid);
                                    }
                                }
                                if (f.is_required == 1 && string.IsNullOrEmpty(hidControl.Value))
                                {
                                    msg = f.title + "不能为空";
                                }

                                if (!string.IsNullOrEmpty(f.valid_pattern))
                                {
                                    if (!Common.Validator.IsMatch(hidControl.Value.ToString(), f.valid_pattern.Substring(1, f.valid_pattern.Length - 2)))
                                    {
                                        msg = f.title + "填写不符合规则" + (string.IsNullOrEmpty(f.valid_tip_msg) ? "" : "," + f.valid_tip_msg);
                                    }
                                }
                            }
                        }
                        break;
                }
            }
            return dic;
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd(out string msg)
        {
            msg = string.Empty;
            bool result = false;
            Entitys.form model = new Entitys.form();
            BLL.form bll = new BLL.form();
            
            model.data_name = txtDataName.Text.Trim();

            model.site_id = Utils.ObjToInt(this.formModel.site_id);
            model.form_id = this.form_id;
            model.sort_id = Utils.StrToInt(txtSortId.Text.Trim(), 99);
            model.delete_flag = 0;
            model.add_time = model.update_time = DateTime.Now;
            model.add_user = model.update_user = GetAdminInfo().id;
            model.id = bll.Add(model);
            model.fields = SetFieldValues(this.form_id,out msg); //扩展字段赋值
            if (!string.IsNullOrEmpty(msg)) 
            {
                bll.Delete(this.form_id, model.id);
                result = false;
            }
            if (bll.Update(model))
            {
                AddAdminLog(DTEnums.ActionEnum.Add.ToString(), "添加" + this.formModel.title + "数据内容:" + model.data_name); //记录日志
                result = true;
            }
            else
            {
                bll.Delete(this.form_id, model.id);
                result = false;
            }
            return result;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id,out string msg)
        {
            msg = string.Empty;
            bool result = false;
            BLL.form bll = new BLL.form();
            Entitys.form model = bll.GetModel(this.form_id, _id);

            model.data_name = txtDataName.Text.Trim();

            model.site_id = Utils.ObjToInt(this.formModel.site_id);
            model.sort_id = Utils.StrToInt(txtSortId.Text.Trim(), 99);
            model.form_id = this.form_id;
            model.delete_flag = 0;
            model.update_time = DateTime.Now;
            model.update_user = GetAdminInfo().id;

            model.fields = SetFieldValues(this.form_id,out msg); //扩展字段赋值
            if (!string.IsNullOrEmpty(msg))
            {
                result = false;
            }
            else
            {
                if (bll.Update(model))
                {

                    AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), "修改" + this.formModel.title + "数据内容:" + model.data_name); //记录日志
                    result = true;
                }
            }
            return result;
        }
        #endregion

        //保存
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
            {
                ChkAdminLevel("form_data_" + this.form_name, DTEnums.ActionEnum.Edit.ToString()); //检查权限
                string msg = string.Empty;
                if (!DoEdit(this.id,out msg))
                {
                    if (string.IsNullOrEmpty(msg))
                    {
                        JscriptMsg("保存过程中发生错误啦！", string.Empty);
                        return;
                    }
                    else
                    {
                        JscriptMsg(msg, string.Empty);
                        return;
                    }
                }
                JscriptMsg("保存成功！", "data_list.aspx?form_id=" + this.form_id);
            }
            else //添加
            {
                ChkAdminLevel("form_data_" + this.form_name, DTEnums.ActionEnum.Add.ToString()); //检查权限
                string msg = string.Empty;
                if (!DoAdd(out msg))
                {
                    if (string.IsNullOrEmpty(msg))
                    {
                        JscriptMsg("保存过程中发生错误！", string.Empty);
                        return;
                    }
                    else
                    {
                        JscriptMsg(msg, string.Empty);
                        return;
                    }
                }
                JscriptMsg("添加成功！", "data_list.aspx?form_id=" + this.form_id);
            }
        }

    }
}